"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 系统内部使用工具
Case Name   : 使用gs_trace工具开始记录trace
Create At   : 2021/01/25
Owner       : opentestcase015
Description :
    1.查看gaussdb端口号
    2.启动trace
Expect      :
    1.查看gaussdb端口号成功
    2.启动trace成功
History     :
    modified by opentestcase001 at 2021/11/1:优化用例适配版本代码变更
    modified by opentestcase036 at 2022/03/16:优化用例，解决远程调用lsof工具的问题以便能成功启动trace
    modified by opentestcase012 at 2022/05/25:优化用例,规避cfg文件相关问题
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()


class SystemInternalTools(unittest.TestCase):
    def setUp(self):
        LOG.info('-------------------this is setup--------------------')
        LOG.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.common = Common()
        self.pri_root_node = Node('PrimaryRoot')
        self.PrimaryNode = Node('PrimaryDbUser')
        self.sh_primary = CommonSH('PrimaryDbUser')

    def test_system_internal_tools(self):
        text = '-----删除后缀为端口号的cfe文件-----'
        LOG.info(text)
        cfe_file = os.path.join(
            '/', 'dev', 'shm', f'gstrace_trace_cfg_{self.PrimaryNode.db_port}')
        rm_cmd = f'rm -rf {cfe_file}'
        LOG.info(rm_cmd)
        self.common.get_sh_result(self.pri_root_node, rm_cmd)
        check_cmd = f'if [ -d {cfe_file} ]; ' \
            f'then echo "exists"; else echo "not exists"; fi'
        check_msg = self.common.get_sh_result(self.pri_root_node, check_cmd)
        self.assertEqual('not exists', check_msg, '执行失败:' + text)

        text = '-----重启数据库-----'
        LOG.info(text)
        status = self.sh_primary.get_db_cluster_status('detail')
        LOG.info(status)
        self.sh_primary.restart_db_cluster()
        status = self.sh_primary.get_db_cluster_status('detail')
        LOG.info(status)
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + text)

        LOG.info('-----------------查看数据库端口号---------------')
        show_port = f'''show port;'''
        res = self.sh_primary.execut_db_sql(show_port)
        self.node_port = res.splitlines()[-2].strip()
        LOG.info('数据库端口为:' + self.node_port)

        LOG.info('--------------启动trace------------------')
        start_cmd = f'''source {macro.DB_ENV_PATH};
            source /etc/profile
            gstrace start -p {self.node_port};
            '''
        LOG.info(start_cmd)
        start_msg = self.PrimaryNode.sh(start_cmd).result()
        LOG.info(start_msg)
        self.assertIn(self.constant.trace_start_success, start_msg)

    def tearDown(self):
        LOG.info('--------------this is tearDown--------------')
        LOG.info('--------------关闭trace------------------')
        stop_cmd = f'''source {macro.DB_ENV_PATH};
            gstrace stop -p {self.node_port};
            '''
        LOG.info(stop_cmd)
        stop_msg = self.PrimaryNode.sh(stop_cmd).result()
        LOG.info(stop_msg)
        LOG.info(f'-----{os.path.basename(__file__)} end-----')
